package com.xindaoapp.happypet.fragments;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.AddressInfo;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 收货人信息列表
 * 
 * @author chenghao
 * 
 */
public class ConsigneeIntegralFragment extends BaseFragment {
    private ListView mListView;
    private RelativeLayout no_result;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_consignee, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        no_result = (RelativeLayout) view.findViewById(R.id.no_result);
        mListView = (ListView) view.findViewById(R.id.listview);
    }

    @Override
    protected void setRightViewListener(View view) {
        super.setRightViewListener(view);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startFragment(new NewConsigneeFragment());
            }
        });
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        getMoccaApi().getMyAddress(UserState.getUserUid(), new IRequest<List<AddressInfo>>() {

            @Override
            public void request(List<AddressInfo> t) {
                if (null == getActivity()) {
                    return;
                }
                if (null != t) {
                    animation.stop();
                    fragment.setVisibility(View.GONE);
                    if (null == mListView.getAdapter()) {
                        no_result.setVisibility(View.GONE);
                        mListView.setAdapter(new ConsigneeAdapter(t));
                    } else {
                        ((ConsigneeAdapter) mListView.getAdapter()).resetList(t);
                        ((ConsigneeAdapter) mListView.getAdapter()).notifyDataSetChanged();
                    }
                    onDataArrived(true);
                } else {
                    no_result.setVisibility(View.GONE);
                    onDataArrived(false);
                }
            }
        });
    }

    class ConsigneeAdapter extends BaseAdapter {
        private List<AddressInfo> list;

        public ConsigneeAdapter(List<AddressInfo> list) {
            super();
            this.list = list;
        }

        public void resetList(List<AddressInfo> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView =
                        LayoutInflater.from(getActivity()).inflate(R.layout.item_consignee, null);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.phoneNumber = (TextView) convertView.findViewById(R.id.phone_number);
                holder.address = (TextView) convertView.findViewById(R.id.address);
                holder.checkBox = (ImageView) convertView.findViewById(R.id.check_box);
                holder.edit = convertView.findViewById(R.id.edit);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final AddressInfo info = list.get(position);
            holder.name.setText(info.username);
            holder.phoneNumber.setText(info.mobile);
            holder.address.setText(info.address);
            int isdefault = 0;
            try {
                isdefault = Integer.parseInt(info.isdefault);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            if (isdefault == 1) {
                holder.checkBox.setSelected(true);
                // holder.checkBox.setImageResource(R.drawable.check_selected);
            } else {
                holder.checkBox.setSelected(false);
                // holder.checkBox.setImageResource(R.drawable.check_all_normal);
            }
            holder.edit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constants.KEY_CONSIGNEE, info);
                    startFragment(new NewConsigneeFragment(), bundle);
                }
            });

            holder.checkBox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // 设置为默认
                    getMoccaApi().setDefaultAddressDetail(UserState.getUserUid(), info.address_id,
                            new IRequest<Boolean>() {

                                @Override
                                public void request(Boolean t) {
                                    if (getActivity() == null) {
                                        return;
                                    }
                                    if (t) {
                                        Toast.makeText(getActivity(), "设置默认地址成功！",
                                                Toast.LENGTH_SHORT).show();
                                        // 刷新填写订单页面
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable(Constants.KEY_CONSIGNEE, info);
                                        setBundle(bundle);
                                        onLoadData();
                                        mActivity.finish();
                                    } else {
                                        Toast.makeText(getActivity(), "设置默认地址失败！",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            });
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // if (getArguments() == null
                    // || getArguments().getBoolean(Constants.KEY_IS_BACK, true)) {
                    // // 返回上一个界面。并传值给前一个界面
                    // Bundle bundle = new Bundle();
                    // bundle.putSerializable(Constants.KEY_CONSIGNEE, info);
                    // setBundle(bundle);
                    // backFragment();
                    // }

                    // 设置为默认
                    getMoccaApi().setDefaultAddressDetail(UserState.getUserUid(), info.address_id,
                            new IRequest<Boolean>() {

                                @Override
                                public void request(Boolean t) {
                                    if (getActivity() == null) {
                                        return;
                                    }
                                    if (t) {
                                        Toast.makeText(getActivity(), "设置默认地址成功！",
                                                Toast.LENGTH_SHORT).show();
                                        // 刷新填写订单页面
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable(Constants.KEY_CONSIGNEE, info);
                                        setBundle(bundle);
                                        onLoadData();

                                        mActivity.finish();
                                    } else {
                                        Toast.makeText(getActivity(), "设置默认地址失败！",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            });
            return convertView;
        }

        class ViewHolder {
            TextView name;
            TextView phoneNumber;
            TextView address;
            ImageView checkBox;
            View edit;
        }

    }

    @Override
    protected void onFragmentResult(Bundle bundle) {
        super.onFragmentResult(bundle);
        // 刷新数据
        onLoadData();
    }

    @Override
    protected String getTitleName() {
        return "收货人信息";
    }

    @Override
    protected String getRightName() {
        return "新建";
    }

    @Override
    protected boolean showRightView() {
        return true;
    }

}
